package com.ouken.com;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.asynTask.AsynRegister;
//import com.ouken.tool.MplayerTool;
import com.ouken.tool.MyApp;

public class RegisterActivity extends Activity {
	private MyApp appState;
	private SharedPreferences userInfo;
	private RelativeLayout RL_bg ;
	private	ImageButton button_register;
	private	ImageButton button_back ;
	private	ImageButton button_album ;
	private	ImageButton button_camera ;
	private	EditText et_email;
	private	EditText et_passwd;
	private	EditText et_repasswd;
	private	EditText et_nickname;
	private	ImageView userimage;
	//private MplayerTool mplayerTool ;
	private Context context;
	
	
	
	private void initView(){
		userInfo = getSharedPreferences("user_info", 0);
		appState = ((MyApp)getApplicationContext());
		//appState.setMusic(userInfo.getString("MUSIC", "ON").equals("ON"));
		appState.addActivity(RegisterActivity.this);
		
		RL_bg = (RelativeLayout)findViewById(R.id.relativeLayout_register_bg);
        button_register = (ImageButton)findViewById(R.id.registerActiviy_ib_register);
        button_back = (ImageButton)findViewById(R.id.registerActiviy_ib_back);
        button_album = (ImageButton)findViewById(R.id.registerActiviy_ib_album);
        button_camera = (ImageButton)findViewById(R.id.registerActiviy_ib_camera);
        
        et_email = (EditText)findViewById(R.id.registerActiviy_et_email);
        et_passwd = (EditText)findViewById(R.id.registerActiviy_et_passwd);
        et_repasswd = (EditText)findViewById(R.id.registerActiviy_et_retypepasswd);
        et_nickname = (EditText)findViewById(R.id.registerActiviy_et_nickname);
        userimage = (ImageView)findViewById(R.id.registerActiviy_iv_userimage);
		
        //mplayerTool = new MplayerTool(RegisterActivity.this);
        context = RegisterActivity.this;
	}
	
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.register);
	        initView();
	        
	        
	        
	        RL_bg.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(RegisterActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				}
			});
	        button_register.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//mplayerTool.playButtonPressed(RegisterActivity.this);
					//spool.play(hit, 2, 1, 0, 0, (float)1.4);
					
					SoundPlayer.playSound(R.raw.keypress_common);
			        //WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);   
			        
			        
			        /****************************input check***************************************/
			        String checkResult = "OK";
			        String errorMessage = "" ;
			        if(et_email.getText().length() == 0){
			        	checkResult = "ERROR";
			        	errorMessage += "email is empty!\n";
			        }
			        if(et_passwd.getText().length() == 0){
			        	checkResult = "ERROR";
			        	errorMessage += "password is empty!\n";
			        }
			        if(et_repasswd.getText().length() == 0){
			        	checkResult = "ERROR";
			        	errorMessage += "re-type password is empty!\n";
			        }
			        if(et_nickname.getText().length() == 0){
			        	checkResult = "ERROR";
			        	errorMessage += "nickname is empty!\n";
			        }
			        if(!et_passwd.getText().toString().equals(et_repasswd.getText().toString())){
			        	checkResult = "ERROR";
			        	errorMessage += "twice passwords \nare not  the same!\n";
			        }
			        if(checkResult.equals("ERROR")){
				        /*AlertDialog alertDialog =
							new AlertDialog.Builder(RegisterActivity.this)
			                .setIcon(R.drawable.alert_dialog_icon)
			                .setTitle("ERROR!")
			                .setMessage(errorMessage)
			                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
			                    public void onClick(DialogInterface dialog, int whichButton) {
	
			                        //* User clicked OK so do some stuff 
			        				
			                    }
			                }).create();
						
							alertDialog.show();*/
			        	AlertDialog_ok myDialog = new AlertDialog_ok(RegisterActivity.this,errorMessage);
			        	final AlertDialog now = myDialog.AlertDialog_ok_show();
					    
			        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
							@Override  
						    public void onClick(View v) { 
								SoundPlayer.playSound(R.raw.keypress_common);
								/* User clicked OK so do some stuff*/
								  now.dismiss(); 
								} 
							}
						);  	
							
						return;
						
			        }
						
			        
			       
			      
			        /****************************change bitmap to String***************************************/
			        
			        userimage.setDrawingCacheEnabled(true);
			        Bitmap userimg_obmp = Bitmap.createBitmap(userimage.getDrawingCache());
			        userimage.setDrawingCacheEnabled(false);
			        
			        
			        ByteArrayOutputStream out = new ByteArrayOutputStream();   
			        userimg_obmp.compress(Bitmap.CompressFormat.PNG, 100, out);
			        
			        
			        try {   
			            out.flush();   
			            out.close();   
			        } catch (IOException e) {   
			            e.printStackTrace();   
			        }  
			        
			        byte[] bb = out.toByteArray();
			        String test ="";
			        BigInteger bigInt = new BigInteger(1, bb);
			        test = bigInt.toString(16);
			        /****************************remove the space from input data***************************************/
			        String tmp = et_email.getText().toString();
			        et_email.setText(tmp.trim());
			        
			        tmp = et_nickname.getText().toString();
			        et_nickname.setText(tmp.trim());
			        
			        /****************************transport data to the server***************************************/
					new AsynRegister( context,  appState, et_email.getText().toString(), et_passwd.getText().toString(),
							et_nickname.getText().toString(), test,userimg_obmp).execute();
						
						
						
					
					
					
				}
					
				
			});
	        
	        button_back.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					SoundPlayer.playSound(R.raw.keypress_common);
					//mplayerTool.playButtonPressed(RegisterActivity.this);
					RegisterActivity.this.finish();
				}
			});
	        
	       button_album.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					SoundPlayer.playSound(R.raw.keypress_common);
					//mplayerTool.playButtonPressed(RegisterActivity.this);
					
					Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
				    intent.addCategory(Intent.CATEGORY_OPENABLE);
				    intent.setType("image/*");
				    intent.putExtra("crop", "true");
				    intent.putExtra("aspectX", 1);
				    intent.putExtra("aspectY", 1);
				    intent.putExtra("outputX", 80);
				    intent.putExtra("outputY", 80);
				    intent.putExtra("return-data", true);

				    startActivityForResult(intent, 1);
				}
			});
	       button_camera.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(RegisterActivity.this);
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1);
				
			}
		});
	    }
	 @Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 
		 if(requestCode == 1){	
			if(data!=null&&data.getExtras()!=null){
				Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
				ImageView userimage = (ImageView)findViewById(R.id.registerActiviy_iv_userimage);
				userimage.setImageBitmap(cameraBitmap);
    	  }
      }
	  super.onActivityResult(requestCode, resultCode, data);

	 }

}
